package com.yuke.cloud.service.uac.controller.frontend;

import com.yuke.cloud.common.base.constant.GlobalConstant;
import com.yuke.cloud.common.util.wrapper.WrapMapper;
import com.yuke.cloud.common.util.wrapper.Wrapper;
import com.yuke.cloud.service.uac.entity.AccGroup;
import com.yuke.cloud.service.uac.service.AccGroupService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.MediaType;
import com.yuke.cloud.common.core.support.BaseController;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
* Created by wg on 2018/09/15.
*/
@Api(value = "WEB - AccGroupController", tags = "处理AccGroup相关请求", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@RestController
@RequestMapping("/acc/group")
public class AccGroupController extends BaseController {
    @Resource
    private AccGroupService accGroupService;

    @PostMapping
    @ApiOperation(httpMethod = "POST", value = "新增一条记录")
    public Wrapper add(@ApiParam(name = "accGroup", value = "该数据记录的POJO") @RequestBody AccGroup accGroup) {
        //accGroupService.save(accGroup);
        accGroupService.saveGroup(accGroup);
        return WrapMapper.ok(accGroup.getGroupId());
    }

    @DeleteMapping("/{id}")
    @ApiOperation(httpMethod = "DELETE", value = "根据id删除一条记录")
    public Wrapper delete(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        //accGroupService.deleteById(id);
        //return WrapMapper.ok();
        int result = accGroupService.deleteGroupById(id);

        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, "操作失败");
        } else {
            return WrapMapper.wrap(Wrapper.SUCCESS_CODE, "操作成功");
        }
    }

    @PutMapping
    @ApiOperation(httpMethod = "PUT", value = "修改一条记录")
    public Wrapper update(@ApiParam(name = "accGroup", value = "需要修改的内容") @RequestBody AccGroup accGroup) {
        //accGroupService.update(accGroup);
        accGroupService.saveGroup(accGroup);
        return WrapMapper.ok();
    }

    @GetMapping("/{id}")
    @ApiOperation(httpMethod = "GET", value = "根据ID获取具体信息")
    public Wrapper<AccGroup> detail(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        AccGroup accGroup = accGroupService.findById(id);
        return WrapMapper.ok(accGroup);
    }

    @GetMapping
    @ApiOperation(httpMethod = "GET", value = "分页获取所有记录")
    public Wrapper<PageInfo<AccGroup>> list(@ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                                    @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        //mod by wg 20181124 不显示普通用户组
        Example example = new Example(AccGroup.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andNotEqualTo("groupId", GlobalConstant.Sys.GROUP_CUSTOMER);
        List<AccGroup> list = accGroupService.selectByExample(example);
        PageInfo pageInfo = new PageInfo(list);

        return WrapMapper.ok(pageInfo);
    }

    @GetMapping("/childList/{parentId}")
    @ApiOperation(httpMethod = "GET", value = "根据父ID获取对应的子组织，不获取孙子及以下组织")
    public Wrapper<List<AccGroup>> getChildGroups(@ApiParam(name = "parentId", value = "组织父id") @PathVariable Long parentId) {
        List<AccGroup> list = accGroupService.getChildGroups(parentId);
        return WrapMapper.ok(list);
    }

    @GetMapping("/parentList/{groupId}")
    @ApiOperation(httpMethod = "GET", value = "根据组织ID递归获取对应的父组织及自身")
    public Wrapper<List<AccGroup>> getParentGroups(@ApiParam(name = "groupId", value = "组织id") @PathVariable Long groupId) {
//        if (null != groupId){
//            groupId = getLoginAuthDto().getGroupId();
//        }
        List<AccGroup> list = accGroupService.queryGroupListRecurDesc(groupId);

        // add by wg 20190114 将自身也返回
        AccGroup accGroup = accGroupService.findById(groupId);

        if (accGroup != null) {
            list.add(accGroup);
        }

        return WrapMapper.ok(list);
    }

    @GetMapping("/groupInfo")
    @ApiOperation(httpMethod = "GET", value = "分页获取所有记录")
    public Wrapper<PageInfo<AccGroup>> grouplist(@ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                            @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        //mod by wg 20181124 不显示普通用户组
        Example example = new Example(AccGroup.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andNotEqualTo("groupId", GlobalConstant.Sys.GROUP_CUSTOMER);
        List<AccGroup> list = accGroupService.selectByExample(example);
        PageInfo pageInfo = new PageInfo(list);

        return WrapMapper.ok(pageInfo);
    }
}
